import axios from '@/axios/http'
import _ from 'lodash'
import { querystring } from 'vux'
import QS from 'qs'
let moduleEdit =   {
  namespaced:true,
  state: {
    isEditing:true,
    qntoken:'',
    alerdySave:false,
    data:{
      baseLists:{
        firstName:'王妮',
        job:'资深客户经理',
        telephone:13788888888,
        wechat:123456789,
        QQ:250866693,
        company:'',
        email:'250866693@qq.com',
        web:'http://www.baidu.com',
        address:'浙江省义乌市城中西路201号',
        persons:'总服务人数',
        personNum:'2800人',
        workYears:'从业年限',
        years:'5年',
        signs:'保险是一种爱！保险是一种责任！',
        bgImg:'http://wx.jlmgps.com/attachment/images/4/2016/11/PHd4E8i4IGg4GU8l8G2N4DmqNIShIa.jpg',
        bgMusic:'http://oq2nk7o59.bkt.clouddn.com/quanshiai.mp3',
        wxErcode:'https://vux.li?x-page=demo_qrcode'
      },
      //视频
      videos:'http://v.qq.com/iframe/player.html?vid=n0541p9sdqo',
      swiperImgs:[
        'http://wx.jlmgps.com/attachment/images/4/2016/11/QC4Jkp3P4JR2DHahuHgDHZsb1Bzajb.jpg',
        'http://wx.jlmgps.com/attachment/images/4/2016/11/QC4Jkp3P4JR2DHahuHgDHZsb1Bzajb.jpg',
      ],
      //荣誉展示
      honorShow:['90后行销冠军扛旗者1','90后行销冠军扛旗者1','90后行销冠军扛旗者1','90后行销冠军扛旗者1','90后行销冠军扛旗者1','90后行销冠军扛旗者1',],
      //公司简介
      companyProfile:['中国平安保险(集团)股份有限公司（以下简称中国平安，公司，集团）于1988年诞生于深圳蛇口，是中国第一家股份制保险企业，至今已发展成为融保险、银行、投资等金融业务为一体的整合、紧密、多元的综合金融服务','中国平安保险(集团)股份有限公司'],
      //企业文化
      companyculture:['企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化企业文化','这个是企业文化']
    },
    },
    mutations: {
      alerdySave(state,payload){
        state.alerdySave = payload
      },
      chooseModels(state,payload){
        state.data.module = payload 
      },
      //更换背景音乐
      changeMusic(state,payload){
        state.data.baseLists.bgMusic = payload
      },
      //更换视频
      changeVideourl(state,val){
      state.data.videos='http://v.qq.com/iframe/player.html?vid='+val
      },
      changeEditor(state,payload){
      state.isEditing = payload
      },
      updateTokens(state,payload){
      state.qntoken = payload
      },
      updateBodys(state,payload){
        if(payload){
          //console.log(payload)
          state.baseLists = payload
        }
      },
      //更改基本信息
      changeBaseLists(state,payload){
        _.forEach(state.data.baseLists,function(item,index){
          if(item==payload.val){
            state.data.baseLists[index] = payload.msg
          }
        })
      },
      //更改段落文字
      changeContains(state,payload){
        if(payload.index=='-1'){
          state.data[payload.mark].push(payload.contains)
        }else{
          state.data[payload.mark][payload.index]=payload.contains
        } 
      },
      //减少段落
      reduceContains(state,payload){
        state.data[payload.mark].splice(payload.index,1)
      },
      changeSwiperImgs(state,payload){
        state.data.swiperImgs[payload.index] = payload.url
      },
      //
      uploadSingleImg(state,payload){
        if(payload.index=='-1'){
          let arr = _.split(payload.mark, '.')
          state.data[arr[0]][arr[1]] = payload.url
        }else{
          state.data[payload.mark][payload.index] = payload.url
        }
      },
      //更换当前位置
      changeAddress(state,payload){
        state.data.baseLists.address = payload
      }
    },
    actions: {
      getModuleData({commit,state},values){
        let data = {
            id: values.id,
            username:values.username
          }
        axios.post('/public/mxcard/Wxcard/getModuleData',querystring.stringify(data))
          .then(function (response) {
            if(response.data.status==200){
              commit('updateBodys',response.data.body)
            }else{
              routers.replace('/login')
            } 
          })
          .catch(function (error) {
            console.log(error);
          });
      },
      saveAllInfo({commit,state},values){
        values.data = state.data

        axios.post('/public/mxcard/Wxcard/saveAllInfo',QS.stringify(values))
          .then(function (response) {
            //console.log(response.data)
            if(response.data.result==200){
              alert('保存成功')
            }else if(response.data.result==300){
              alert('已经保存过，请勿重复操作')
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      },
      sendEmail({commit,state},values){
        //console.log(values)
       let data = {
           username: _.trim(values.username),
           userphone: _.trim(values.userphone),
           usermessage: _.trim(values.usermessage),
           myemail:state.data.baseLists.email, 
         }
       axios.post('/public/mxcard/Wxcard/sendEmailMyself',querystring.stringify(data))
         .then(function (response) {
          alert(response.data)
         })
         .catch(function (error) {
           console.log(error);
         });
      },
      getQiniuTken({ commit, state }, values){
        axios.get('/public/mxcard/Wxcard/getQiniuToken')
          .then(function (response) {
            if(response.data){
            commit('updateTokens',response.data)
            }else{
              alert('操作超时，请重新打开页面')
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      },
      updateDemoPosition ({commit}, top) {
        commit({type: 'updateDemoPosition', top: top})
      },
      getVideoSrc:({ commit, state }, values)=>{
        let data = {
            uncode: '12346788',
            url:values
          }
        axios.post('/public/caiji/Wxcaiji/getSrc',querystring.stringify(data))
          .then(function (response) {
            if(response.data.result==1){
              commit('changeVideourl',response.data.data)
            }else{
              Alert('视频地址有误，请更换地址')
            }
            state.token = data;
          })
          .catch(function (error) {
            console.log(error);
          });
      }
    }
}
export default moduleEdit;